package org.mzy.pet.controller;

import org.mzy.basic.anno.PreAuthorize;
import org.mzy.basic.util.AjaxResult;
import org.mzy.basic.util.LoginContext;
import org.mzy.basic.util.PageList;
import org.mzy.org.domain.Employee;
import org.mzy.org.service.IEmployeeService;
import org.mzy.pet.domain.Pet;
import org.mzy.pet.query.PetQuery;
import org.mzy.pet.service.IPetService;
import org.mzy.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    public IPetService petService;
    @Autowired
    private IEmployeeService employeeService;

    /**
     * 保存和修改公用的
     *
     * @param pet 传递的实体
     * @return Result转换结果
     */
    @PutMapping
    @PreAuthorize(name = "新增/修改宠物", sn = "pet:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request) {
        // 获取当前登录人信息
        Logininfo logininfo = LoginContext.getLoginUser(request);
        // 获取登录人id
        Long id = logininfo.getId();
        // 通过id找到员工信息
        Employee employee = employeeService.selectByLogininfoId(id);
//        // 通过员工id，赋值给pet的userid
//        pet.setUserId(employee.getId());
        pet.setUserId(null);
        // 通过员工的店铺id，赋值给宠物所属店铺
        pet.setShopId(employee.getShopId());
        try {
            if (pet.getId() != null)
                petService.update(pet);
            else
                petService.add(pet);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    /**
     * 删除对象信息
     *
     * @param id
     * @return
     */
    @PreAuthorize(name = "删除宠物", sn = "pet:delete")
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        try {
            petService.delete(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }

    //获取用户
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据id查询宠物", sn = "pet:get")
    public Pet get(@PathVariable("id") Long id) {
        return petService.findById(id);
    }


    /**
     * 查看所有的员工信息
     *
     * @return
     */
    @GetMapping()
    @PreAuthorize(name = "查询全部宠物", sn = "pet:list")
    public List<Pet> list() {
        return petService.findAll();
    }


    /**
     * 分页查询数据
     *
     * @param query 查询对象
     * @return PageList 分页对象
     */
    @PostMapping("/list")
    @PreAuthorize(name = "分页查询全部宠物", sn = "pet:json")
    public PageList<Pet> json(@RequestBody PetQuery query) {
        System.out.println(petService.queryByPage(query));
        return petService.queryByPage(query);
    }

    /*上架*/
    @PostMapping("/onsale")
    @PreAuthorize(name = "上架宠物", sn = "pet:onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        try {
            petService.onsale(ids);
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().success(false).setMsg(e.getMessage());
        }
    }

    /*下架*/
    @PostMapping("/offsale")
    @PreAuthorize(name = "下架宠物", sn = "pet:offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids) {
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().success(false).setMsg(e.getMessage());
        }
    }

    /**
     * 根据Id查询宠物详情
     *
     * @param id
     * @return
     */
    @GetMapping("/getPetDetail/{id}")
    @PreAuthorize(name = "根据id查询宠物详情", sn = "pet:getPetDetail")
    public Pet getPetDetail(@PathVariable("id") Long id) {
        return petService.getPetDetail(id);
    }

    /*批量删除*/
    @PatchMapping
    @PreAuthorize(name = "批量删除宠物", sn = "pet:batchDel")
    public AjaxResult batchDel(@RequestBody List<Long> ids) {
        petService.batchDel(ids);
        return AjaxResult.me().success();
    }
}
